home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Unix / CNews / Source / rna / funcs.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-11-21  |  5.7 KB  |  347 lines

  1. #include "defs.h"
  2.  
  3. /*
  4.  * string handling functions
  5.  */
  6. char *
  7. myalloc(size)
  8. int size;
  9. {
  10.     register char *cp;
  11.  
  12.     extern char *malloc();
  13.  
  14.     if ((cp = malloc((unsigned) size)) == NIL(char))
  15.         error("No more memory.");
  16.     return cp;
  17. }
  18.  
  19.  
  20. char *
  21. myrealloc(ptr, size)
  22. char *ptr;
  23. int size;
  24. {
  25.     register char *cp;
  26.  
  27.     extern char *realloc();
  28.  
  29.     if ((cp = realloc(ptr, (unsigned) size)) == NIL(char))
  30.         error("No more memory.");
  31.     return cp;
  32. }
  33.  
  34.  
  35. char *
  36. newstr(s)
  37. char *s;
  38. {
  39.     return strcpy(myalloc(strlen(s) + 1), s);
  40. }
  41.  
  42.  
  43. char *
  44. newstr2(s1, s2)
  45. char *s1, *s2;
  46. {
  47.     return strcat(strcpy(myalloc(strlen(s1) + strlen(s2) + 1), s1), s2);
  48. }
  49.  
  50.  
  51. char *
  52. newstr3(s1, s2, s3)
  53. char *s1, *s2, *s3;
  54. {
  55.     return strcat(strcat(strcpy(myalloc(strlen(s1) + strlen(s2) + strlen(s3) +
  56.         1), s1), s2), s3);
  57. }
  58.  
  59.  
  60. char *
  61. newstr4(s1, s2, s3, s4)
  62. char *s1, *s2, *s3, *s4;
  63. {
  64.     return strcat(strcat(strcat(strcpy(myalloc(strlen(s1) + strlen(s2) +
  65.         strlen(s3) + strlen(s4) + 1), s1), s2), s3), s4);
  66. }
  67.  
  68.  
  69. char *
  70. newstr5(s1, s2, s3, s4, s5)
  71. char *s1, *s2, *s3, *s4, *s5;
  72. {
  73.     return strcat(strcat(strcat(strcat(strcpy(myalloc(strlen(s1) + strlen(s2) +
  74.         strlen(s3) + strlen(s4) + strlen(s5) + 1), s1), s2), s3), s4), s5);
  75. }
  76.  
  77.  
  78. char *
  79. newstr6(s1, s2, s3, s4, s5, s6)
  80. char *s1, *s2, *s3, *s4, *s5, *s6;
  81. {
  82.     return strcat(strcat(strcat(strcat(strcat(strcpy(myalloc(strlen(s1) +
  83.         strlen(s2) + strlen(s3) + strlen(s4) + strlen(s5) + strlen(s6) + 1),
  84.          s1), s2), s3), s4), s5), s6);
  85. }
  86.  
  87.  
  88. char *
  89. catstr(old, s)
  90. char *old, *s;
  91. {
  92.     return strcat(myrealloc(old, strlen(old) + strlen(s) + 1), s);
  93. }
  94.  
  95.  
  96. char *
  97. catstr2(old, s1, s2)
  98. char *old, *s1, *s2;
  99. {
  100.     return strcat(strcat(myrealloc(old, strlen(old) + strlen(s1) + strlen(s2) +
  101.         1), s1), s2);
  102. }
  103.  
  104.  
  105. /*
  106.  * News group matching.
  107.  *
  108.  * nglist is a list of newsgroups.
  109.  * sublist is a list of subscriptions.
  110.  * sublist may have "meta newsgroups" in it.
  111.  * All fields are NGSEPCHAR separated.
  112.  *
  113.  * sublist uses "all" like shell uses "*", and "." like shell uses "/"
  114.  * if subscription X matches Y, it also matches Y.anything
  115.  */
  116. ngmatch(nglist, sublist)
  117. char *nglist, *sublist;
  118. {
  119.     register char *n, *s, *nd, *sd;
  120.     register int rc;
  121.  
  122.     rc = 0;
  123.     n = nglist;
  124.     while (*n && rc == 0) {
  125.         if (nd = strchr(n, NGSEPCHAR))
  126.             *nd = '\0';
  127.         s = sublist;
  128.         while (*s) {
  129.             if (sd = strchr(s, NGSEPCHAR))
  130.                 *sd = '\0';
  131.             if (*s != NEGCHAR)
  132.                 rc |= ptrncmp(s, n);
  133.             else
  134.                 rc &= ~ptrncmp(s + 1, n);
  135.             if (sd)
  136.                 *sd = NGSEPCHAR, s = sd + 1;
  137.             else
  138.                 break;
  139.         }
  140.         if (nd)
  141.             *nd = NGSEPCHAR, n = nd + 1;
  142.         else
  143.             break;
  144.     }
  145.     return rc;
  146. }
  147.  
  148.  
  149. /*
  150.  * Compare two newsgroups for equality.
  151.  * The first one may be a "meta" newsgroup.
  152.  */
  153. static
  154. ptrncmp(ng1, ng2)
  155. register char *ng1, *ng2;
  156. {
  157.  
  158.     while (1) {
  159.         if (ng1[0] == 'a' && ng1[1] == 'l' && ng1[2] == 'l' && (ng1[3] ==
  160.             '\0' || ng1[3] == '.')) {
  161.             if (ng1[3] == '\0')    /* "all" matches anything */
  162.                 return 1;
  163.             while (*ng2 && *ng2 != '.')
  164.                 ng2++;
  165.             if (*ng2 != '.')        /* "all." doesn't match "xx" */
  166.                 return 0;
  167.             ng1 += 4, ng2++;
  168.             continue;
  169.         }
  170.         while (*ng1 && *ng1 != '.' && *ng1 == *ng2)
  171.             ng1++, ng2++;
  172.         if (*ng1 == '.') {
  173.             if (*ng2 != '.' && *ng2 != '\0')
  174.                 return 0;    /* "."'s don't line up */
  175.             if (*ng2)
  176.                 ng2++;
  177.             ng1++;            /* "."'s line up - keep going */
  178.         } else if (*ng1 == '\0')
  179.             return (*ng2 == '\0' || *ng2 == '.');
  180.             /* full match or X matching X.thing */
  181.         else
  182.             return 0;
  183.     }
  184.     /* NOTREACHED */
  185. }
  186.  
  187.  
  188.  
  189. /*
  190.  * open a file
  191.  */
  192. FILE *
  193. fopenf(name, mode)
  194. char *name, *mode;
  195. {
  196.     register FILE    *f;
  197.  
  198.     if ((f = fopen(name, mode)) == NULL)
  199.         error("Can't %s %s", *mode == 'r' ? "open" : "create", name);
  200.     return f;
  201. }
  202.  
  203.  
  204. /*
  205.  * replace all '.''s with '/'
  206.  */
  207. char *
  208. convg(s)
  209. register char *s;
  210. {
  211.     register char *sav;
  212.  
  213.     sav = s;
  214.     while (s = strchr(s, '.'))
  215.         *s = '/';
  216.     return sav;
  217. }
  218.  
  219.  
  220. /*
  221.  * get a line from stdin
  222.  * trim leading and trailing blanks
  223.  */
  224. char *
  225. mgets()
  226. {
  227.     register char *s;
  228.     static char buf[BUFSIZ];
  229.  
  230.     fflush(stdout);
  231.     if (fgets(buf, sizeof(buf), stdin) == NULL) {
  232.         (void) printf("\n");
  233.         return NIL(char);
  234.     }
  235.     if (s = strchr(buf, '\n'))
  236.         while (isspace(*s) && s > buf)
  237.             *s-- = '\0';
  238.     else {
  239.         (void) printf("Input line too long.\n");
  240.         return NULL;
  241.     }
  242.     s = buf;
  243.     while (isspace(*s))
  244.         s++;
  245.     return s;
  246. }
  247.  
  248.  
  249. /*
  250.  * apply the given function to each member in the newsgroup
  251.  */
  252. /* VARARGS2 */
  253. applyng(ng, func, arg1)
  254. register char *ng;
  255. register int (*func)();
  256. char *arg1;
  257. {
  258.     register char *delim;
  259.     register int err;
  260.  
  261.     err = 0;
  262.     while (*ng) {
  263.         if (delim = strchr(ng, NGSEPCHAR))
  264.             *delim = '\0';
  265.         err += (*func)(ng, arg1);
  266.         if (delim)
  267.             *delim = NGSEPCHAR, ng = delim + 1;
  268.         else
  269.             break;
  270.     }
  271.     return err;
  272. }
  273.  
  274.  
  275. /*
  276.  * generate a return address
  277.  */
  278. char *
  279. getretaddr(hp)
  280. header *hp;
  281. {
  282.     register char *ra;
  283.  
  284.     extern char *exaddress();
  285.  
  286.     if (hp->h_replyto)
  287.         ra = exaddress(hp->h_replyto);
  288.     else if (hp->h_from)
  289.         ra = exaddress(hp->h_from);
  290.     else
  291.         ra = NIL(char);
  292.     if (hp->h_path && !ra)
  293.         ra = hp->h_path;
  294.     return ra;
  295. }
  296.  
  297.  
  298. /*
  299.  * try and make a proper address
  300.  */
  301. char *
  302. exaddress(addr)
  303. char *addr;
  304. {
  305.     register char *space, *dot, *at;
  306.     register char *raddr;
  307.     extern char mailvia[];
  308.  
  309.     raddr = NIL(char);
  310.     if (strcmp(mailvia, "<path>") == 0)
  311.         return raddr;
  312.     if (space = strchr(addr, ' '))
  313.         *space = '\0';
  314.     if (mailvia[0] != '\0' && (at = strchr(addr, '@')) != NULL) {
  315.         *at = '\0';
  316.         raddr = newstr5(mailvia, PSEPS, at + 1, PSEPS, addr);
  317.         *at = '@';
  318.     } else
  319.         raddr = newstr(addr);
  320.     if (space)
  321.         *space = ' ';
  322.     return raddr;
  323. }
  324.  
  325.  
  326. /*
  327.  * remove extra spaces, and insert separators if necessary in
  328.  * newsgroups specification
  329.  */
  330. convgrps(sp)
  331. register char *sp;
  332. {
  333.     register char *sep = NULL;
  334.  
  335.     while (*sp) {
  336.         if (sep)
  337.             sp++;
  338.         while (*sp && (isspace(*sp) || *sp == NGSEPCHAR))
  339.             strcpy(sp, sp + 1);
  340.         if (sep)
  341.             *sep = (*sp ? NGSEPCHAR : '\0');
  342.         while (*sp && !isspace(*sp) && *sp != NGSEPCHAR)
  343.             sp++;
  344.         sep = sp;
  345.     }
  346. }
  347.